//
//  DetailPictureViewController.m
//  iNews
//
//  Created by lanou3g on 14-12-10.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "DetailPictureViewController.h"
#import "ShareDataHandle.h"
#import "ArchiveHandleSingle.h"

#import "DetailModel.h"
#import "DetailArchiveModel.h"
#import "UIImage+ImageDown.h"

#import "DetailPicView.h"
#import "ArchiveHandleSingle.h"
#import "DetailArchiveModel.h"

//static NSString *kButtonText = @"收藏";

//分别为空的,实的,没圆圈的
static NSString *imag1 = @"icon_littlesStar@2x.png";
static NSString *imag2 = @"icon_star_full@2x.png";
//static NSString *imag3 = @"icon_star123_yellow@2x.png";



@interface DetailPictureViewController () <UIScrollViewDelegate>



@end

@implementation DetailPictureViewController
- (void)dealloc
{
    [_dPicView release];
    [_postStr release];
    
    [_dArr release];
    [_im release];
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        //观察字符串有没有变化
        [self addObserver:self forKeyPath:@"postStr" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
        
        
        self.dPicView = [[[DetailPicView alloc] initWithFrame:self.view.bounds]autorelease ];
        
    }
    return self;
}

- (void)loadView
{
    self.view = self.dPicView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
   // self.im = [UIImage imageNamed:imag1];
    
    [self.dPicView.leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.dPicView.rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.dPicView.scroll.delegate = self;
    
}
//观察者方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
    
    //判断button的title是收藏还是取消收藏
    if ([keyPath isEqualToString:@"postStr"]) {
        
        [[ArchiveHandleSingle archiveSingle] searchPicture];
        int t = 0;
        
        if ([ArchiveHandleSingle archiveSingle].pictureArray.count != 0) {
            NSLog(@"%@",[ArchiveHandleSingle archiveSingle].pictureArray);
            
            for (DetailArchiveModel *mo in [ArchiveHandleSingle archiveSingle].pictureArray) {
           //     NSLog(@"%@",mo);
                
                if ([mo.postid isEqualToString:_postStr]) {
                   // kButtonText = @"取消收藏";
                    self.im = [UIImage imageNamed:imag2];
                    [self.dPicView.rightButton setImage:self.im forState:UIControlStateNormal];
                    
                    t = 1;
                    
            //        NSLog(@"%@",mo);
                    break;
                }
                
            }
        }
        if (!t) {
            
           // kButtonText = @"收藏";
            self.im = [UIImage imageNamed:imag1];
            
            [self.dPicView.rightButton setImage:self.im forState:UIControlStateNormal];
        }
        
        //请求数据
        [[ShareDataHandle shareDataHandle] parsePicViewDataWithString:_postStr];
        
        //KVO
        [[ShareDataHandle shareDataHandle] addObserver:self forKeyPath:@"picArray" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

    }
    
    if ([keyPath isEqualToString:@"picArray"]) {

        DetailModel *dm = [ShareDataHandle shareDataHandle].picArray.lastObject;
        
        //在滑动图片是调用
        _temp = dm.photos.count;
       // _dArr = [NSArray arrayWithArray:dm.photos];
        
        [self.dPicView setMessageWithModel:[ShareDataHandle shareDataHandle].picArray.lastObject];
        
        [[ShareDataHandle shareDataHandle] removeObserver:self forKeyPath:@"picArray"];
    }
    
}

- (void)leftButtonAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//右边button的方法实现(收藏,取消收藏)
- (void)rightButtonAction:(UIButton *)sender
{
 //   NSLog(@"%@",self.dPicView.array);
    
    //图片收藏
    //本页面有的值
   
    //  dam.noteArray = [NSMutableArray arrayWithArray:self.dPicView.noteArr];
   // NSLog(@"%@",self.im);
    
    [[ArchiveHandleSingle archiveSingle] searchPicture];
    
    if ([self.im isEqual:[UIImage imageNamed:imag1]]) {
        
        //取到model类
        DetailModel *dModel = [ShareDataHandle shareDataHandle].picArray.lastObject;
        
        if (dModel != nil) {
            
            //将数据存到model类
            DetailArchiveModel *dam = [[[DetailArchiveModel alloc] init] autorelease];
            dam.setname = dModel.setname;
            dam.postid = self.postStr;
            dam.imageArray = [NSMutableArray arrayWithArray:self.dPicView.array];
            
            //放到子线程中归档防止界面卡顿现象(但是再点击就会崩)
//            dispatch_queue_t myQueue = dispatch_queue_create("iNews.com", DISPATCH_QUEUE_CONCURRENT);
//            dispatch_async(myQueue, ^{
//                
//                [[ArchiveHandleSingle archiveSingle] archivePicture:dam];
//            });
            
            [[ArchiveHandleSingle archiveSingle] archivePicture:dam];
            // kButtonText = @"取消收藏";
            
            [self addAlterView];
            self.im = [UIImage imageNamed:imag2];
            
            [self.dPicView.rightButton setImage:self.im forState:UIControlStateNormal];
            //  NSLog(@"存本地了");

        }else{
            
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"暂无网络连接" message:@"无法收藏" delegate:self cancelButtonTitle:nil
                                               otherButtonTitles:nil, nil];
            [av show];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [av dismissWithClickedButtonIndex:0 animated:YES];
                [av release];
            });

        }
        
        
    }else{
        
        for (DetailArchiveModel *dam in [ArchiveHandleSingle archiveSingle].pictureArray) {
            
            if ([dam.postid isEqualToString:self.tempStr]) {
                
                //放到子线程中
                
               [[ArchiveHandleSingle archiveSingle] deletePictureView:dam];
               
                break;
            }
        }
        
      //  kButtonText = @"收藏";
        [self addAlterView];
        self.im = [UIImage imageNamed:imag1];
        [self.dPicView.rightButton setImage:self.im forState:UIControlStateNormal];
        
      //  NSLog(@"删除突击了");
    }
    
}

//传参方法
/*
- (void)passValue:(NSString *)s
{
    self.postStr = s;
    self.tempStr = s;
    
    //解析图片展示页的数据
    [[ShareDataHandle shareDataHandle] parsePicViewDataWithString:s];
    
    //KVO
    [[ShareDataHandle shareDataHandle] addObserver:self forKeyPath:@"picArray" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

}
*/

//scroll减速完成的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    DetailModel *dm = [ShareDataHandle shareDataHandle].picArray.lastObject;
    
    int t = scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width;
    self.dPicView.numLab.text = [NSString stringWithFormat:@"%d/%ld",t+1,(long)_temp];
    
    self.dPicView.textV.text = dm.photos[t][@"note"];
}


#pragma mark 添加弹窗
- (void)addAlterView
{
    if ([self.im isEqual:[UIImage imageNamed:imag1]]) {
        
        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"收藏成功!" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        
        [alertV show];
        
    }else{
        
        UIAlertView *aV = [[UIAlertView alloc] initWithTitle:@"取消成功" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        
        [aV show];
    }
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
